unit IUFamilia;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TFormIUFamilia = class(TForm)
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    LabelCliente: TLabel;
    EditNome: TEdit;
    Label1: TLabel;
    ComboBoxAtivo: TComboBox;
    Label2: TLabel;
    EditOrdem: TEdit;
    procedure BitBtn2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure EditOrdemKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
    xModo:string;
    xId:integer;
  end;

var
  FormIUFamilia: TFormIUFamilia;
  sql:string;

implementation

uses Principal;

{$R *.dfm}

procedure TFormIUFamilia.BitBtn2Click(Sender: TObject);
begin
    self.close;
end;

procedure TFormIUFamilia.FormShow(Sender: TObject);
begin
 if xModo = 'U' then
    begin
       sql := 'select * from familia where id = ' + trim(IntToStr(xId));

       FormPrincipal.QueryAux.SQL.Clear;
       FormPrincipal.QueryAux.SQL.add(sql);
       FormPrincipal.QueryAux.Close;
       FormPrincipal.QueryAux.Open;

       EditNome.Text :=  FormPrincipal.QueryAux.FieldByName('nm').AsString;
       EditOrdem.Text :=  FormPrincipal.QueryAux.FieldByName('nr_ordem').AsString;

       ComboBoxAtivo.ItemIndex := FormPrincipal.QueryAux.FieldByName('ativo').asinteger;

    end;
end;

procedure TFormIUFamilia.BitBtn1Click(Sender: TObject);
begin
 if xModo = 'I' then
       sql := 'insert into familia (nm,nk,nr_ordem,ativo,dt_cadastro) values (''' + EditNome.Text + ''',''' + copy(EditNome.Text,1,20) + ''',''' + EditNome.Text + ''','+ IntToStr(ComboBoxAtivo.ItemIndex) + ',now())'
    else
       sql := 'update familia set nm = ''' + EditNome.Text + ''' , nk = ''' + copy(EditNome.Text,1,20) + ''', nr_ordem = ' + EditOrdem.Text + ', ativo = ' + IntToStr(ComboBoxAtivo.ItemIndex) + ' where id = ' + trim(IntToStr(xId));


    FormPrincipal.QueryAtualiza.SQL.Clear;
    FormPrincipal.QueryAtualiza.SQL.add(sql);
    FormPrincipal.QueryAtualiza.ExecSQL;

    self.Close;
end;

procedure TFormIUFamilia.EditOrdemKeyPress(Sender: TObject; var Key: Char);
begin
if not(key in ['0'..'9',#8,#13]) then
   key := #0;

if key in [',','.'] then 
   key := DecimalSeparator;

if key = #13 then
begin
   SelectNext(Sender as tWinControl, True, True );
   Key := #0;
end;
end;

end.
